package com.cn.springcloudwechat.springUtil;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.context.support.ServletContextResource;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.XmlViewResolver;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletContext;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

@Configuration
public class WebMvcStaticConfig implements WebMvcConfigurer {
	@Autowired
	ServletContext servletContext;


	/**
	 * 利用fastjson替换掉jackson，且解决中文乱码问题
	 * 
	 * @param converters
	 */
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
		// 处理中文乱码问题
		List<MediaType> fastMediaTypes = new ArrayList<>();
		fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
		fastConverter.setSupportedMediaTypes(fastMediaTypes);
		fastConverter.setFastJsonConfig(fastJsonConfig);
		converters.add(fastConverter);
	}

	/**
	 * 路径映射
	 * @param registry
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// 将所有/static/** 访问都映射到classpath:/static/ 目录下
		registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
		registry.addResourceHandler("/img/**").addResourceLocations("classpath:templates/img/");
		registry.addResourceHandler("/assets/**").addResourceLocations("classpath:templates/assets/");
		registry.addResourceHandler("/html/**").addResourceLocations("classpath:templates/html/");
		registry.addResourceHandler("/js/**").addResourceLocations("classpath:static/js/");
		registry.addResourceHandler("/css/**").addResourceLocations("classpath:static/css/");
		//registry.addResourceHandler("/system/**").addResourceLocations("file:webapp/WEB-INF/views/system/");
		registry.addResourceHandler("/**").addResourceLocations("/");
	}

	/**
	 * 模板配置
	 * @return
	 */
	@Bean
	FreeMarkerConfigurer FreeMarkerConfigurer(){
		FreeMarkerConfigurer freeMarkerConfigurer=new FreeMarkerConfigurer();
		freeMarkerConfigurer.setTemplateLoaderPath("classpath:/templates/");
		freeMarkerConfigurer.setDefaultEncoding("UTF-8");
		return freeMarkerConfigurer;
	}
	/**
	 * html解析器模板配置
	 * @return
	 */
	@Bean(name = "htmlviewResolver")
	FreeMarkerViewResolver htmlviewResolver(){
		FreeMarkerViewResolver freeMarkerViewResolver=new FreeMarkerViewResolver();
		freeMarkerViewResolver.setSuffix(".html");
		freeMarkerViewResolver.setOrder(0);
		freeMarkerViewResolver.setContentType("text/html;charset=UTF-8");
		freeMarkerViewResolver.setCache(false);
		return freeMarkerViewResolver;
	}

	/**
	 * jsp解析器配置
	 * @return
	 */
	@Bean
	InternalResourceViewResolver jspViewResolver(){
		final InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		viewResolver.setPrefix("/WEB-INF/views/");
		viewResolver.setSuffix(".jsp");
		viewResolver.setViewNames("*");
		viewResolver.setOrder(1);
		viewResolver.setContentType("text/html;charset=UTF-8");
		return viewResolver;

	}

	/**
	 * 文件上传大小
	 * @return
	 */
	@Bean
	MultipartConfigElement multipartConfigElement(){
		MultipartConfigFactory factory = new MultipartConfigFactory();
		//文件最大
		factory.setMaxFileSize("10240MB"); //KB,MB
		/// 设置总上传数据总大小
		factory.setMaxRequestSize("102400MB");
		return factory.createMultipartConfig();
	}


	@Override
	public void configurePathMatch(PathMatchConfigurer configurer) {
		// 常用的两种
		// 匹配结尾 / :会识别 url 的最后一个字符是否为 /
		// localhost:8080/test 与 localhost:8080/test/ 等价
		configurer.setUseTrailingSlashMatch(true);
		// 匹配后缀名：会识别 xx.* 后缀的内容
		// localhost:8080/test 与 localhost:8080/test.jsp 等价
		configurer.setUseSuffixPatternMatch(true);

		configurer.setUseSuffixPatternMatch(true).setUseTrailingSlashMatch(true);
	}

}